﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace VTFC
{
    // ----------------------------------------------------------------------------
    /// <summary>
    /// <see cref="VTFC.ServerContainer"/> manager class.
    /// </summary>
    /// <remarks>
    /// Load and save ServerContainer instance.
    /// </remarks>
    // ----------------------------------------------------------------------------
    public class ServerManager
    {
        /// <summary>Helper object for locking.</summary>
        private static object olock = new Object();
        /// <summary>Path of the Server.xml file.</summary>
        private static string xmlpath = null;
        /// <summary><see cref="VTFC.ServerContainer"/> instance.</summary>
        public static ServerContainer ServerContainer { get; set; }
        
        // ----------------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        // ----------------------------------------------------------------------------
        static ServerManager()
        {
            xmlpath = Path.Combine(Environment.GetFolderPath(
                Environment.SpecialFolder.LocalApplicationData), 
                Constant.VTFC_APPLICATION_SMALL_NAME + "\\" + Constant.VTFC_SERVERS_FILE);

            Load();
        }//ctor

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Load <see cref="VTFC.ServerContainer"/> from file system or create a new one instance.
        /// </summary>
        // ----------------------------------------------------------------------------
        public static void Load()
        {
            lock (olock)
            {
                if (File.Exists(xmlpath))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ServerContainer));
                    using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(xmlpath)))
                    {
                        ServerContainer = serializer.Deserialize(ms) as ServerContainer;
                    }
                }
                else
                {
                    ServerContainer = new ServerContainer();
                    ServerContainer.Servers = new List<Server>();
                }
            }
        }//func Load

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Save <see cref="VTFC.ServerContainer"/> instance.
        /// </summary>
        // ----------------------------------------------------------------------------
        public static void Save()
        {
            lock (olock)
            {
                BaseLogic.CreateDirectory(Path.GetDirectoryName(xmlpath));

                ServerContainer.Servers = ServerContainer.Servers.OrderBy(s => s.Url).ToList();

                XmlSerializer serializer = new XmlSerializer(typeof(ServerContainer));
                using (MemoryStream ms = new MemoryStream())
                {
                    serializer.Serialize(ms, ServerContainer);
                    File.WriteAllBytes(xmlpath, ms.ToArray());
                }
            }
        }//func Save

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Get the default server instance if exists.
        /// </summary>
        /// <returns>Default <see cref="VTFC.Server"/> instance or null.</returns>
        // ----------------------------------------------------------------------------
        public static Server GetDefaultServer()
        {
            lock (olock)
            {
                Server server = null;
                if (!String.IsNullOrEmpty(ServerContainer.DefaultServerUrl))
                {
                    server = ServerContainer.Servers.Find(s => s.Url.Equals(ServerContainer.DefaultServerUrl));
                }
                return server;
            }
        }//func GetDefaultServer

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Get a <see cref="VTFC.Server"/> instance by the Url property.
        /// </summary>
        /// <param name="url">TFS server url.</param>
        /// <returns><see cref="VTFC.Server"/> instance.</returns>
        // ----------------------------------------------------------------------------
        public static Server GetServerByUrl(string url)
        {
            lock (olock)
            {
                Server server = null;
                if (!String.IsNullOrEmpty(url))
                {
                    server = ServerContainer.Servers.Find(s => s.Url.Equals(url));
                }
                return server;
            }
        }//func GetServerByUrl
    }//class ServerManager
}//namespace VTFC
